<?php

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\SMTP;
require './PHPMailer-master/src/Exception.php';
require './PHPMailer-master/src/PHPMailer.php';
require './PHPMailer-master/src/SMTP.php';

session_start();
require_once("database.php");
$conn=mysqli_connect(HOSTNAME,
    USERNAME,
    PASSWORK,
    DATABASE,
    PORT);
$data=date("Y-m-d H:i:s");
$sql5= "SELECT borrowrecords.ReturnDate,users.Email FROM borrowrecords INNER JOIN users on borrowrecords.UserID = users.UserID WHERE re = 1 AND ReturnDate < '$data' ";;
$sql5= mysqli_query($conn,$sql5);
//$res5=mysqli_fetch_all($sql5,MYSQLI_ASSOC);
$mail = new PHPMailer(true);
$data1 = "请尽快归还图书";
try {
        $mail->SMTPDebug = SMTP::DEBUG_SERVER;                      //Enable verbose debug output 启用详细调试输出
        $mail->isSMTP();                                            //Send using SMTP
        $mail->Charset='UTF-8';
        $mail->Host = 'smtp.163.com';                     //Set the SMTP server to send through 将SMTP服务器设置为通过
        $mail->SMTPAuth = true;                                   //Enable SMTP authentication 启用SMTP身份验证
        $mail->Username = 'liangxinming_2008@163.com';                     //SMTP username
        $mail->Password = 'UKTKDDZQURQIFESC';                               //SMTP password
        $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;            //Enable implicit TLS encryption 启用隐式TLS加密
        $mail->Port = 465;                                    //TCP port to connect to; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS`
        $mail->setFrom('liangxinming_2008@163.com', '图书管理员');
    while ($res=$sql5->fetch_assoc()) {
        $mail->addAddress($res['Email'], $res['Name']);     //Add a recipient 添加收件人
    }
        $mail->addReplyTo('liangxinming_2008@163.com', 'server');//回复地址
//        $mail->addBCC('liangxinming_2008@qq.com');
        $mail->isSMTP(true);                                  //Set email format to HTML 将电子邮件格式设置为HTML
        $mail->Subject = "=?utf-8?B?" . base64_encode("过期提醒") . "?=";  // 标题
        $mail->Body =$data1;
        $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';//这是非HTML邮件客户端的纯文本正文
        $mail->send();
        return '发送成功';

} catch (Exception $e) {
    return "发送失败: {$mail->ErrorInfo}";
}
?>
